﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BLL;

namespace DAL
{
    public class RoleRepository : IRoleRepository
    {
        public List<BLL.Role> Search(string roleName, string firstName, string lastName, string status, string authorities, string sortExpression, string sortDirection, int PageSize, int PageIndex, out int pageCnt)
        {

            //添加参数
            SqlParameter[] parameters = new SqlParameter[]{            
                 new SqlParameter("@rolenamepar", SqlDbType.NVarChar,50) ,
                 new SqlParameter("@statusnamepar", SqlDbType.NVarChar,50) ,
                 new SqlParameter("@firstnamepar", SqlDbType.NVarChar,50) ,
                 new SqlParameter("@lastnamepar", SqlDbType.NVarChar,50) ,
                 new SqlParameter("@authoritylistpar", SqlDbType.NVarChar,300) ,
                 new SqlParameter("@sortExpressionpar", SqlDbType.NVarChar,50) ,
                 new SqlParameter("@sortDirectionpar", SqlDbType.NVarChar,50) ,
                 new SqlParameter("@PageSize", SqlDbType.Int),
                 new SqlParameter("@PageIndex", SqlDbType.Int),
            };
            //给参数赋值
            parameters[0].Value = roleName;
            parameters[1].Value = status;
            parameters[2].Value = firstName;
            parameters[3].Value = lastName;
            parameters[4].Value = authorities;
            parameters[5].Value = sortExpression;
            parameters[6].Value = sortDirection;
            parameters[7].Value = PageSize;
            parameters[8].Value = PageIndex;

            DataTable searchDataTable = DbHelperSql.Execproce("rolesearchprocedure3", parameters);

            List<BLL.Role> roleList = new List<BLL.Role>();
            foreach (DataRow dr in searchDataTable.Rows)
            {
                //string n = dr["RoName"].ToString();

                //Role role = new Role();
                //role.Id = (int)(dr["Id"]);出错：Specified cast is not valid.
                //role.RoName = (string)dr["RoName"];
                //role.AuName = (string)dr["AuName"];
                //role.LeName = (string)dr["leName"];
                //string n = dr["RoName"].ToString();

                BLL.Role role = new BLL.Role();
                role.RId = Convert.ToInt32(dr["RId"]);
                role.RoName = dr["RoName"].ToString();
                role.LeName = dr["leName"].ToString();
                role.AuName = dr["AuName"].ToString();
                //role.Authority.AuthorityName = dr["AuName"].ToString();                
                role.PageCount = Convert.ToInt32(dr["page_num"]);

                roleList.Add(role);

            }

            if (roleList.Count > 0)
            {
                pageCnt = roleList[0].PageCount;
            }
            else
            {
                pageCnt = 0;
            }
            return roleList;
        }

    }
}
